#pragma once

namespace catan
{
	template<class Serializer>
	class Receiver
	{
	public:
		~Receiver()
		{
			SafeDeleteAll(mFunctions);
		}

		template <class Serializer>
		class IFunctionBase
		{
		public:
			virtual void OnReceive(Serializer& ss) = 0;
		};

		template<class Protocol>
		void Reg(IFunctionBase<Serializer>* Function)
		{
			mFunctions.insert(std::make_pair(Protocol::TypeValue, Function));
		}

		template<class Protocol>
		void UnReg()
		{
			mFunctions.erase(Protocol::TypeValue);
		}
		
		template <class Serializer>
		void OnReceive(Serializer& ss)
		{
			int header = 0;
			ss >> header;
			VALID (mFunctions.find(header) != mFunctions.end());
			mFunctions[header]->OnReceive(ss);
		}


		std::map<int, IFunctionBase<Serializer>*> mFunctions;
	};
}	